package com.example.xianji.activity;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;
import com.example.xianji.widgets.X5WebView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Main_Skill_DetailsActivity extends Activity implements OnClickListener {
    public Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };
    /*************************技术详情页*******************************/
    private Button skill_detail_upload, skill_zixun;
    private ImageView skill_detail_back, skill_iamge_collection;
    private TextView skill_details_title, skill_details_tag;
    private X5WebView skill_details_webview;
    //    private SharedPreferences sp;
//    private Editor ed;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__skill__details);
        /*******************初始化************************/
        init();

        XJApplication.cachThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                get_skill_details_data();
            }
        });

        skill_detail_back.setOnClickListener(this);
        skill_detail_upload.setOnClickListener(this);
        skill_zixun.setOnClickListener(this);
        skill_iamge_collection.setOnClickListener(this);
    }

    private void init() {
        skill_detail_upload = (Button) findViewById(R.id.skill_detail_upload_button);
        skill_detail_back = (ImageView) findViewById(R.id.skill_detail_back);
        skill_zixun = (Button) findViewById(R.id.skill_zixun);
        skill_details_title = (TextView) findViewById(R.id.skill_details_title);
        skill_details_tag = (TextView) findViewById(R.id.skill_details_tag);
        skill_details_webview = (X5WebView) findViewById(R.id.skill_details_webview);
        skill_iamge_collection = (ImageView) findViewById(R.id.skill_iamge_collection);
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.skill_detail_upload_button) {
            if (PreferenceEngine.getInstance().getBoolean(Constant.SP.NAME, Constant.SP.KEY_XIANJI_DENGLU, false)) {
                startActivity(new Intent(Main_Skill_DetailsActivity.this, Main_UpLoadActivity.class));
            } else {
                Intent intent = new Intent(Main_Skill_DetailsActivity.this, Main_LoginActivity.class);
                intent.putExtra("origin_login", 58);
                startActivity(intent);
            }
        } else if (v.getId() == R.id.skill_detail_back) {
            Main_Skill_DetailsActivity.this.finish();
        } else if (v.getId() == R.id.skill_zixun) {
            if (PreferenceEngine.getInstance().getBoolean(Constant.SP.NAME, Constant.SP.KEY_XIANJI_DENGLU, false)) {
                Intent intent = new Intent(Main_Skill_DetailsActivity.this, Main_Want_ZiXunActivity.class);
                intent.putExtra("skill_id", id);
                intent.putExtra("from_what", 2);
                startActivity(intent);
            } else {
                Intent intent = new Intent(Main_Skill_DetailsActivity.this, Main_LoginActivity.class);
                intent.putExtra("origin_login", 58);
                startActivity(intent);
            }

        } else if (v.getId() == R.id.skill_iamge_collection) {
            if (PreferenceEngine.getInstance().getBoolean(Constant.SP.NAME, Constant.SP.KEY_XIANJI_DENGLU, false)) {
                collection();
            } else {
                startActivity(new Intent(Main_Skill_DetailsActivity.this, Main_LoginActivity.class));
            }
        }
    }

    public void get_skill_details_data() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("api_origin", "origin_android");
        parms.addBodyParameter("achieve_id", getIntent().getStringExtra("skill_id"));
        httpUtils.send(HttpMethod.POST, Contant.skill_details, parms,
                new RequestCallBack<String>() {
                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        try {
                            JSONObject response = new JSONObject(arg0.result);
                            if (response.getInt("status") == 1) {
                                final JSONObject object = response.getJSONObject("data");
                                id = object.getInt("id");
                                runOnUiThread(new Runnable() {
                                    public void run() {
                                        try {
                                            skill_details_title.setText(object.getString("title"));
                                            skill_details_tag.setText("技术分类  : " + object.getJSONObject("parents").getString("name"));
                                            if (skill_details_webview == null) {
                                                skill_details_webview = (X5WebView) findViewById(R.id.skill_details_webview);
                                            }
                                            skill_details_webview.loadDataWithBaseURL(null, object.getString("details"), "text/html", "utf-8", null);

                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    public void collection() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("id", getIntent().getStringExtra("skill_id"));
        parms.addBodyParameter("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        parms.addBodyParameter("title", skill_details_title.getText().toString());
        parms.addBodyParameter("type", "achieve");
        httpUtils.send(HttpMethod.POST, Contant.collection, parms, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                try {
                    JSONObject object = new JSONObject(arg0.result);
                    Toast.makeText(Main_Skill_DetailsActivity.this, object.getString("msg"), Toast.LENGTH_SHORT).show();
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (skill_details_webview != null) {
            skill_details_webview.destroy();
            skill_details_webview = null;
        }
    }
}